************
**	Programs to replicate tables and figures in 
**	"On the measurement of environmental inequality: 
**	Ranking emissions distributions generated by different policy instruments"
**	by Erin Mansur and Glenn Sheriff
************

cd "/Users/Glenn/Dropbox (ASU)/reclaim/replication_files/"

************
**	Table 1
************

capture program drop table1
program table1
	
	use facilities , clear
		
	foreach f in matched entrant ext other {
		glo scenarios nox1 nox2 
		if "`f'" == "matched" {
			local i "reclaim_match == 1"
			glo 	scenarios nox1 nox2 control2  	
		}
		if "`f'" == "entrant"	local i "dumreclaim == 1 & nox1 == . & nox2 ~= ."
		if "`f'" == "ext"		local i "dumreclaim == 1 & nox2 == . & nox1 ~= ."
		if "`f'" == "other"		local i "dumreclaim == 0"
	
		foreach e of glo scenarios {
			summ `e' if `i' 
			foreach s in sum mean sd min max N {
				sca `s'_`e' = r(`s')
			}
		}
		
		if "`f'" == "matched" {	
			file open latex using fac_sum , write replace
				file write latex "\hspace{0.125cm}Total"	"&" %09.1fc (mean_nox1*N_nox1)  "&" 
				file write latex  %09.1fc (mean_control2*N_control2)		"&" %09.1fc (mean_nox2*N_nox2) "\\"
				file write latex  "\hspace{0.125cm}Mean"  	"&"  %09.1fc (mean_nox1)  		"&" 
				file write latex  %09.1fc (mean_control2)  	"&" %09.1fc (mean_nox2) "\\"
				file write latex  "\hspace{0.125cm}Standard Deviation" 		"&"%09.1fc (sd_nox1) "&" 
				file write latex  %09.1fc (sd_control2)  	"&" %09.1fc (sd_nox2) "\\"
				file write latex "\hspace{0.125cm}Coefficient of Variation" "&"%09.1fc (sd_nox1/mean_nox1)  "&" 
				file write latex  %09.1fc (sd_control2/mean_control2)  		"&" %09.1fc (sd_nox2/mean_nox2) "\\"
				file write latex  "\hspace{0.125cm}Minimum" "&" %09.1fc (min_nox1)  		"&" 
				file write latex  %09.1fc (min_control2)  	"&" %09.1fc (min_nox2) "\\"
				file write latex  "\hspace{0.125cm}Maximum" "&" %09.1fc (max_nox1)  		"&" 
				file write latex  %09.1fc (max_control2)  	"&" %09.1fc (max_nox2) "\\" 
				file write latex  "\hspace{0.125cm}N" 		"&" %09.0fc (N_nox1)  "&" %09.0fc (N_control2) "&"
				file write latex  %09.0fc (N_nox2) "\\" _n
			file close latex
		}
		else {
			file open latex using `f'_sum , write replace
				file write latex "\hspace{0.125cm}Total" 	"&" %09.1fc (mean_nox1*N_nox1)  "& &" 
				file write latex  %09.1fc (mean_nox2*N_nox2)  " \\"
				file write latex  "\hspace{0.125cm}Mean"	"&"  %09.1fc (mean_nox1)  "& &" 
				file write latex   %09.1fc (mean_nox2) " \\" 
				file write latex  "\hspace{0.125cm}N" 		"&" %09.0fc (N_nox1)  "& &" %09.0fc (N_nox2) " \\" _n
			file close latex
		}
	}	
end


************
**	Table 2
************
capture program drop table2
program table2

	foreach y in 1990 2000 {
		use ej, clear
		keep ej* year id
		duplicates drop
		// note: variable ejX denotes population in CBG for group X (see variable labels)
		keep if year == `y'
		forval i = 1/8 {
			summ ej`i'
			foreach s in sum mean sd min max {
				sca `s'_ej`i'`y' = r(`s')
			}
			sca sum_ej`i'`y' = sum_ej`i'`y'/1000000			
		}
	}

	file open latex using race_sum , write replace
	forval i = 2/5 {
		file write latex "\hspace{.25cm}" `"`: var la ej`i''"' "&" %10.1fc (sum_ej`i'1990)  "&" 
		file write latex  %9.0fc 	(mean_ej`i'1990)	"&" %09.0fc (sd_ej`i'1990) "&" 
		file write latex  %10.1fc 	(sum_ej`i'2000)  	"&" 
		file write latex  %09.0fc 	(mean_ej`i'2000) 	"&" %09.0fc (sd_ej`i'2000) "\\"  _n
	}
	file close latex
	file open latex using income_sum , write replace
	forval i = 6/8 {
		file write latex "\hspace{.25cm}" `"`: var la ej`i''"' "&" %10.1fc (sum_ej`i'1990)  "&" 
		file write latex  %09.0fc (mean_ej`i'1990)  "&" %09.0fc (sd_ej`i'1990) "&" 
		file write latex  %10.1fc (sum_ej`i'2000)  "&" 
		file write latex  %09.0fc (mean_ej`i'2000)  "&" %09.0fc (sd_ej`i'2000) "\\"  _n
	}
	file close latex
	loc i 1
	file open latex using total_sum , write replace
		file write latex  `"`: var la ej`i''"' "&" %09.1fc (sum_ej`i'1990)  "&" 
		file write latex  %09.0fc (mean_ej`i'1990)  "&" %09.0fc (sd_ej`i'1990) "&" 
		file write latex  %10.1fc (sum_ej`i'2000)  "&" 
		file write latex  %09.0fc (mean_ej`i'2000)  "&" %09.0fc (sd_ej`i'2000) "\\"  _n
	file close latex
end

************
**	Tables 3, 4, A.1, A.2 
************

// these tables requires following programs:
//	"iap" 			calculate Kolm equivalent inequality aversion parameters from Atkinson
//	"equity" 		generate EDEs and Index values
//	"kolm"			calculate kolm EDE and index
//	"kolm-boot"		bootstrap EDE and index standard errors
//	"table_fill"	repetitive tasks to generate table

// macros:
	global iap   25 50 75 				// low/med/high atkinson equivalent paramters *100
	
	global options nox1 nox2 control2  	// emissions scenarios #=period, control is counterfactual
	
capture program drop iap  
program iap , rclass byable(recall)
	// calculate kolm equivalent inequality aversion parameters, see Eq. (6)
	// arguments:	(1) outcome variable (2) frequency weight 
	//				(3)	Atkinson inequality aversion parameter
	syntax varname [fweight/]  , atkinson_iap(real)
	
		marksample touse
		
		tempvar w x xsq wx  wxsq beta betawx
	 
		gen `w' 	= `exp'
	 
		gen `x' 	= `varlist'
		
		gen `xsq'	= `x' * `x'
	 
		gen `wx'	= `w' * `x'
		
		gen `wxsq'	= `w' * `xsq'
		
		gen `beta' = `atkinson_iap'
		
		gen `betawx' = `beta' * `wx'
		
		tempname m0 wxsqm1 betawxm1 
		
		sum `w' if `touse'
		
		scalar `m0' = r(mean)
		
		sum `betawx' if `touse'

		scalar `betawxm1' = r(mean)
		
		sum `wxsq' if `touse'

		scalar `wxsqm1' = r(mean)
			
		tempname  iap
		
		scalar `iap' = -`betawxm1'/`wxsqm1'
		
		return scalar iap = `iap'
end

capture program drop kolm 
program define kolm, rclass byable(recall)
	//	adapted from Biewen (2002)  
	// https://uni-tuebingen.de/index.php?eID=tx_securedownloads&p=18343&u=0&g=0&t=1572027943&hash=410eab5656890bbfd87eb17b8e679d854b54b736&file=/fileadmin/Uni_Tuebingen/Fakultaeten/WiSo/Wiwi/Uploads/Lehrstuehle/Prof._Biewen/bootstrap.zip */
	// 3 inputs: values, weights, inequality aversion parameter */
	// returns r(gem1) r(mld) r(theil) r(ge2) r(kolm)   */
syntax varlist   [if] , kolm_iap(real)
	tokenize `varlist'

	marksample touse

	quietly {

	// calculate products 

    tempvar w x wx  wexpbx beta

	gen `w' = `2'
	
	// drop missing values
	
	drop if `w' == .
	
	gen `x' = `1'
	
	gen `beta' = `kolm_iap'

    gen `wx' = `w' * `x'

	gen `wexpbx' = `w' * exp(-`beta' * `x')

    /* calculate moments */

    tempname m0 m1  eta mean 

    sum `w' if `touse'

    scalar `m0' = r(mean)

    sum `wx' if `touse'

    scalar `m1' = r(mean)
	
	sum `x' [fw=`2'] if `touse'
	
	scalar `mean' = r(mean)
	
	count if `wexpbx' == .
	}
	if r(N) > 0 di "Warning missing values"
	
	quietly {
	
	sum `wexpbx' if `touse'
	
	scalar `eta' = r(mean)

    /* calculate indices */

    tempname  index ede

	scalar `index' = -`m1'/`m0'-1/`beta'*ln(`eta'/`m0')
	
	scalar `ede' = -1/`beta'*ln(`eta'/`m0')

    /* return results */
	
	return scalar index = `index'
	return scalar ede = `ede'
	return scalar mean = `mean'
	
	}
	
end

capture program drop kolm_boot
program define kolm_boot, rclass
	foreach k of global iap {
		forval i = 1/9 {
			foreach opt of glo options {				
				kolm `opt' ej`i'  , kolm_iap(${iap`k'})
				return scalar index_`opt'_ej`i'_`k'	= r(index)
				return scalar EDE_`opt'_ej`i'_`k' 	= r(ede)
				return scalar xbar_`opt'_ej`i'_`k'	= r(mean)
			}
		}		
	}
end

capture program drop equity  
program equity
	// calculates EDEs, means, indexes
	
	//  calculate Kolm for 1990 demographic data, assuming 3-km radius dispersion 
	//	for 3 Atkinson values
	foreach ip of glo iap {
		loc atk = `ip'/100
		use ej , clear
		keep if year == 1990
		keep if dispersion == 3
		iap nox1 [fweight = ej1] , atkinson_iap(`atk')
		glo iap`ip' = r(iap)
	}	
		
	foreach yy in 1990 2000 {
		glo ej
		foreach ip of glo iap {	
			forval i=1/9{			
				foreach j in xbar EDE index {
					global ej $ej r(`j'_nox1_ej`i'_`ip') r(`j'_nox2_ej`i'_`ip') r(`j'_control2_ej`i'_`ip') `j'_diff_ej`i'_`ip' = (r(`j'_nox2_ej`i'_`ip')-r(`j'_control2_ej`i'_`ip'))
				}
			}
		}
	
		use ej , clear
		keep if dispersion == 3 & year == `yy'
		bootstrap $ej , rep(1000) : kolm_boot
		 	
		matrix se = e(se)
	
		glo c = 1
	// get std errors from bootstrap output matrix
		foreach ip of glo iap {
			forval i = 1/9 {
				foreach j in xbar EDE index  {
					foreach k in nox1 nox2 control2 diff {
						scalar `j'`ip'_`k'_ej`i'`yy'_se = se[1, $c ]
						glo c = $c + 1
					}
				}
			}
		}
	}
	// calculate kolm statistics one last time without bootstrap to obtain
	// main point estimates
	
	foreach ip of glo iap {
		foreach yy in 1990 2000 {
			forval i=1/9{
				foreach opt of glo options {
					use ej , clear
					keep if dispersion == 3 & year == `yy'
					kolm `opt'  ej`i'  , kolm_iap(${iap`ip'})
					sca EDE`ip'_`opt'_ej`i'`yy' = r(ede)
					sca index`ip'_`opt'_ej`i'`yy' = r(index)
					sca	xbar`ip'_`opt'_ej`i'`yy' = r(mean)
				}
			}
		}
	}	
			
end

capture program drop table_fill
program table_fill
	local bold1
	local bold2
	if "$i" == "1" {
		local bold1 "\bftab " 
		local bold2 ""
	}
	file write latex2 "`bold1'" %09.1fc (${j}_nox1_ej${i}${yy}) "`bold2' &" "`bold1'" %09.1fc (${j}_control2_ej${i}${yy}) "`bold2' &" 
	file write latex2 "`bold1'" %09.1fc (${j}_nox2_ej${i}${yy}) "`bold2' &" "`bold1'" %09.1fc (${j}_nox2_ej${i}${yy}-${j}_control2_ej${i}${yy})   "`bold2' \\" _n 
	file write latex2 "& `bold1' (" %09.1fc (${j}_nox1_ej${i}${yy}_se) ") `bold2' &" "`bold1' (" %09.1fc (${j}_control2_ej${i}${yy}_se) ") `bold2' &" 
	file write latex2 "`bold1' (" %09.1fc (${j}_nox2_ej${i}${yy}_se) ") `bold2' &" "`bold1' (" %09.1fc (${j}_diff_ej${i}${yy}_se)   ") `bold2' \\" _n
end

capture program drop ede_tables
program ede_tables
	equity
	//use file only to get variable labels
	use ej , clear
	keep if year == 1990 & dispersion == 3
	capture file close latex2
	foreach ip of glo iap {
		foreach j in xbar`ip' EDE`ip' index`ip'  {		
			glo ip `ip'
			foreach yy in 1990 2000 {
				global j `j'
				global yy `yy'
					
				file open latex2 using `j'_`yy'.txt, write replace
				file write latex2 "\hspace{.0125\linewidth}Race/Ethnicity \\" _n
				forval i=2/5 {
					global i `i'
					file write latex2 "\hspace{.025\linewidth}" `"`: var la ej`i''"' "&" 
					table_fill
				}
			
				file write latex2 "\hspace{.0125\linewidth}Income \\" _n
						
				forval i=6/8 {
					global i `i'
					file write latex2 "\hspace{.025\linewidth}" `"`: var la ej`i''"' "&" 
					table_fill
				}				 
			
				foreach i of num 1 {
					global i `i'
					file write latex2 " \textbf{ " `"`: var la ej`i''"' "} &"  
					table_fill
				}
				file close latex2		
			}
		}
	}			

end



